﻿/**
 ** @作者:	   王桂龙 QQ731513122
 ** @日期:	   2019.12.6
 ** @简介:     tabControl控件
 **            可以在任何TabAlignment模式正常工作
 **            实现对tapges内子控件继承于IBHXJZoom接口的，非Dock类的控件进行缩放控制          
 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace BHXJDOTNET.OLD
{
    public partial class BHXJTabControl : TabControl, IBHXJZOOM,IBHXJCONTAINER
    {
        #region 继承于ZOOM接口的属性和方法
        private Point _OriginPoint = new Point(0, 0);
        [Description("原点(慎用,在足够了解BHXJZoom的情况下使用)"), Category("BHXJZoom")]
        /// <summary>
        /// 原点
        /// </summary>
        public Point OriginPoint
        {
            get
            { return this._OriginPoint; }
            set
            {
                if (this._OriginPoint != value)
                {
                    this._OriginPoint = value;
                    if (DesignMode)
                        this.IniLocation = this.BHXJGetIniLocationInDesignMode();
                }
            }
        }
        private bool _AutoOrigin = false;
        [Description("自动更改原点模式(只在设计模式下有用)"), Category("BHXJZoom")]
        /// <summary>
        /// 自动更改原点模式(只在设计模式下有用)
        /// </summary>
        public bool AutoOrigin
        {
            get
            {
                return this._AutoOrigin;
            }
            set
            {
                if (this._AutoOrigin != value)
                {
                    this._AutoOrigin = value;
                    if (DesignMode)
                    {
                        if (this.AutoOrigin)
                        {
                            this.OriginPoint = this.Location;
                        }
                        else
                        {
                            this.OriginPoint = new Point(0, 0);
                        }
                    }
                }
            }
        }
        /// <summary>
        /// 初始尺寸
        /// </summary>
        [Browsable(false)]
        public Size IniSize { get; set; }
        /// <summary>
        /// 初始位置
        /// </summary>
        [Browsable(false)]
        public Point IniLocation { get; set; }
        /// <summary>
        /// 初始字体
        /// </summary>
        [Browsable(false)]
        public Font IniFont { get; set; }

        #endregion

        #region 屏蔽掉的属性
        [Browsable(false)]
        public new TabAppearance Appearance
        {
            get { return base.Appearance; }
            set { if (base.Appearance != value) base.Appearance = value; }
        }
        [Browsable(false)]
        public new TabDrawMode DrawMode
        {
            get { return base.DrawMode; }
            set { if (base.DrawMode != value) base.DrawMode = value; }
        }
        [Browsable(false)]
        public new TabSizeMode SizeMode
        {
            get { return base.SizeMode; }
            set { if (base.SizeMode != value)  base.SizeMode = value; }
        }
        #endregion


        public new TabAlignment Alignment
        {
            get { return base.Alignment; }
            set
            {
                if (base.Alignment != value)
                {
                    base.Alignment = value;
                    SetItemSize(null);
                }
            }
        }

        /// <summary>
        /// _背景色
        /// </summary>
        private Color _BackgroundColor = Color.Black;
        [Description("背景色"), Category("BHXJ")]
        /// <summary>
        /// 背景色
        /// </summary>
        public Color BackgroundColor
        {
            get
            {
                return this._BackgroundColor;
            }
            set
            {
                if (this._BackgroundColor != value)
                {
                    this._BackgroundColor = value;
                    this.Invalidate(false);
                }
            }
        }

        /// <summary>
        /// _常规状态字体颜色
        /// </summary>
        private Color _NormalFontColor = Color.FromArgb(128, 128, 128);
        [Description("常规状态字体颜色"), Category("BHXJ")]
        /// <summary>
        /// 常规状态字体颜色
        /// </summary>
        public Color NormalFontColor
        {
            get
            {
                return this._NormalFontColor;
            }
            set
            {
                if (this._NormalFontColor != value)
                {
                    this._NormalFontColor = value;
                    this.Invalidate(false);
                }
            }
        }

        /// <summary>
        /// _选中状态字体颜色
        /// </summary>
        private Color _SelectedFontColor = Color.FromArgb(16, 208, 208);
        [Description("选中状态字体颜色"), Category("BHXJ")]
        /// <summary>
        /// 选中状态字体颜色
        /// </summary>
        public Color SelectedFontColor
        {
            get
            {
                return this._SelectedFontColor;
            }
            set
            {
                if (this._SelectedFontColor != value)
                {
                    this._SelectedFontColor = value;
                    this.Invalidate(false);
                }
            }
        }

        /// <summary>
        /// _选中标签颜色
        /// </summary>
        private Color _SelectedTabColor = Color.FromArgb(64, 64, 64);
        [Description("选中标签颜色"), Category("BHXJ")]
        /// <summary>
        /// 选中标签颜色
        /// </summary>
        public Color SelectedTabColor
        {
            get
            {
                return this._SelectedTabColor;
            }
            set
            {
                if (this._SelectedTabColor != value)
                {
                    this._SelectedTabColor = value;
                    this.Invalidate(false);
                }
            }
        }

        /// <summary>
        /// _常规标签颜色
        /// </summary>
        private Color _NormalTabColor = Color.FromArgb(32, 32, 32);
        [Description("常规标签颜色"), Category("BHXJ")]
        /// <summary>
        /// 常规标签颜色
        /// </summary>
        public Color NormalTabColor
        {
            get
            {
                return this._NormalTabColor;
            }
            set
            {
                if (this._NormalTabColor != value)
                {
                    this._NormalTabColor = value;
                    this.Invalidate(false);
                }
            }
        }

        /// <summary>
        /// _有边框
        /// </summary>
        private bool _HasFrame = true;
        [Description("有边框"), Category("BHXJ")]
        /// <summary>
        /// 有边框
        /// </summary>
        public bool HasFrame
        {
            get { return this._HasFrame; }
            set 
            {
                if (this._HasFrame != value)
                {
                    this._HasFrame = value;
                    this.Invalidate(false);
                }
            }
        }

        /// <summary>
        /// _边框颜色
        /// </summary>
        private Color _FrameColor = Color.FromArgb(64, 64, 64);
        [Description("边框颜色"), Category("BHXJ")]
        /// <summary>
        /// 边框颜色
        /// </summary>
        public Color FrameColor
        {
            get
            {
                return this._FrameColor;
            }
            set
            {
                if (this._FrameColor != value)
                {
                    this._FrameColor = value;
                    this.Invalidate(false);
                }
            }
        }

        /// <summary>
        /// _标签形状
        /// </summary>
        private ItemShape _TabShape = ItemShape.Circular;
        [Description("标签形状"), Category("BHXJ")]
        /// <summary>
        /// 标签形状
        /// </summary>
        public ItemShape TabShape
        {
            get
            {
                return this._TabShape;
            }
            set
            {
                if (this._TabShape != value)
                {
                    this._TabShape = value;
                    this.Invalidate(false);
                }
            }
        }

        /// <summary>
        /// 标签圆角半径
        /// </summary>
        private int _TabRadius = 3;
        [Description("标签圆角半径"), Category("BHXJ")]
        /// <summary>
        /// 标签圆角半径
        /// </summary>
        //[Browsable(false)]
        public int TabRadius
        {
            get
            {
                return this._TabRadius;
            }
            set
            {
                if (this._TabRadius != value)
                {
                    int r = (value > 0) ? value : 1;
                    this._TabRadius = r;
                    this.Invalidate(false);
                }
            }
        }

        /// <summary>
        /// _标签横向缩放系数
        /// </summary>
        private float _ItemWidthScale = 1;
        [Description("标签横向缩放系数"), Category("BHXJ")]
        /// <summary>
        /// 标签横向缩放系数
        /// </summary>
        public float ItemWidthScale
        {
            get { return this._ItemWidthScale; }
            set 
            {
                if (this._ItemWidthScale != value)
                {
                    this._ItemWidthScale = value;
                    this.SetItemSize(null);
                }
            }
        }

        /// <summary>
        /// _标签纵向缩放系数
        /// </summary>
        private float _ItemHighScale = 1;
        [Description("标签纵向缩放系数"), Category("BHXJ")]
        /// <summary>
        /// 标签纵向缩放系数
        /// </summary>
        public float ItemHighScale
        {
            get { return this._ItemHighScale; }
            set
            {
                if (this._ItemHighScale != value)
                {
                    this._ItemHighScale = value;
                    this.SetItemSize(null);
                }
            }
        }


        #region 内部IZoom控件缩放
        private bool _ZoomFlag = false;
        [Description("是否对子控件启用Zoom功能"), Category("BHXJZoom")]
        /// <summary>
        /// 是否对子控件启用Zoom功能
        /// </summary>
        public bool ZoomFlag
        {
            get { return this._ZoomFlag; }
            set
            {
                if (value != this._ZoomFlag)
                {
                    this._ZoomFlag = value;
                    //重新缩放
                    this.TabPageRefrush(this.SelectedIndex);
                }
            }
        }

        private bool _ZoomSize = false;
        [Description("（ZoomFlag为真时）缩放子控件尺寸"), Category("BHXJZoom")]
        /// <summary>
        /// （ZoomFlag为真时）缩放子控件尺寸
        /// </summary>
        public bool ZoomSize
        {
            get { return this._ZoomSize; }
            set
            {
                if (value != this._ZoomSize)
                {
                    this._ZoomSize = value;
                    //重新缩放
                    this.TabPageRefrush(this.SelectedIndex);
                }
            }
        }

        private bool _ZoomFont = false;
        [Description("（ZoomFlag为真时）缩放子控件字体"), Category("BHXJZoom")]
        /// <summary>
        /// （ZoomFlag为真时）缩放子控件字体
        /// </summary>
        public bool ZoomFont
        {
            get { return this._ZoomFont; }
            set
            {
                if (value != this._ZoomFont)
                {
                    this._ZoomFont = value;
                    //重新缩放
                    this.TabPageRefrush(this.SelectedIndex);
                }
            }
        }

        private bool _ZoomLocation = false;
        [Description("（ZoomFlag为真时）缩放子控件位置"), Category("BHXJZoom")]
        /// <summary>
        /// （ZoomFlag为真时）缩放子控件位置
        /// </summary>
        public bool ZoomLocation
        {
            get { return this._ZoomLocation; }
            set
            {
                if (value != this._ZoomLocation)
                {
                    this._ZoomLocation = value;
                    //重新缩放
                    this.TabPageRefrush(this.SelectedIndex);

                }
            }
        }
        #endregion
        /// <summary>
        /// 内部尺寸
        /// </summary>
        [Browsable(false)]
        public Size InnerSize
        {
            get;
            set;
        }


        private int mousemoveindex = -1;

        public BHXJTabControl()
        {
            this.SetStyle(ControlStyles.UserPaint, true);
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.DoubleBuffer, true);
            //this.SetStyle(ControlStyles.ResizeRedraw, true);
            this.SetStyle(ControlStyles.ResizeRedraw, false);
            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);

            #region 设计时,变化事件
            if (System.ComponentModel.LicenseManager.UsageMode == System.ComponentModel.LicenseUsageMode.Designtime)
            {
                //设计时事件
                this.SizeChanged += new EventHandler(this.BHXJ_SizeChangedInDesignMode);
                this.FontChanged += new EventHandler(this.BHXJ_FontChangedInDesignMode);
                this.LocationChanged += new EventHandler(this.BHXJ_LocationChangedInDesignMode);

                //innerSize类
                this.SizeChanged += new EventHandler(this.SizeChangedInner);
            }
            #endregion

            //变化事件
            this.FontChanged += new EventHandler(BHXJ_FontChanged);

            //标签尺寸模式改为fixed
            this.SizeMode = TabSizeMode.Fixed;

            //标签可叠加
            this.Multiline = true;

            //防止字体在设计器中添加该控件时候自动追随父控件
            this.Font = this.Font;

            //设置初始
            this.IniSize = this.Size;
            this.IniFont = this.Font;
            this.IniLocation = this.BHXJGetIniLocationInDesignMode();

            this.InnerSize = this.Size;
            //尺寸变化事件
            this.SizeChanged += this.Control_SizeChanged;

            this.MouseLeave += BHXJTabControl_MouseLeave;
            this.MouseMove += BHXJTabControl_MouseMove;
        }

        private void BHXJTabControl_MouseMove(object sender, MouseEventArgs e)
        {
            int temp = -1;
            for (int i = 0; i < this.TabPages.Count; i++)
            {
                Rectangle rect = this.GetTabRect(i);
                if (rect.Contains(e.Location))
                {
                    temp = i;
                    break;
                }
            }
            if (temp != this.mousemoveindex)
            {
                this.mousemoveindex = temp;
                this.Invalidate(false);
            }
        }

        private void BHXJTabControl_MouseLeave(object sender, EventArgs e)
        {
            this.mousemoveindex = -1;
            this.Invalidate(false);
        }

        private void SizeChangedInner(object sender, EventArgs e)
        {
            if (this.DesignMode)
            {
                if (this.TabPages.Count > 0)
                {
                    this.InnerSize = this.TabPages[this.SelectedIndex].ClientSize;
                }
                else
                    this.InnerSize = this.Size;
            }
        }

        /// <summary>
        /// 尺寸变化事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Control_SizeChanged(object sender, EventArgs e)
        {
            this.Invalidate(false);
        }

        private void TabPage_SizeChanged(object sender, EventArgs e)
        {
            TabPage tp = sender as TabPage;
            TabPageChanged(tp);
        }

        /// <summary>
        /// page刷新
        /// </summary>
        /// <param name="index"></param>
        public void TabPageRefrush(int index)
        {
            try
            {
                TabPage tp = this.TabPages[index];
                if (tp != null)
                {
                    this.TabPageChanged(tp);
                }
            }
            catch
            { }
        }

        /// <summary>
        /// 恢复原有尺寸位置字体
        /// </summary>
        /// <param name="tp"></param>
        private void TabPageReset(TabPage tp)
        {
            if (this.DesignMode)
                return;
            foreach (Control con in tp.Controls)
            {
                if (con.Dock == DockStyle.None)
                {
                    if (con is IBHXJZOOM)
                    {
                        IBHXJZOOM zoom = con as IBHXJZOOM;
                        zoom.OriginPoint = new Point(0, 0);
                        zoom.BHXJLocationZoom(1);
                        zoom.BHXJSizeZoom(1);
                        zoom.BHXJFontZoom(1);
                    }
                }
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="tp"></param>
        private void TabPageChanged(TabPage tp)
        {
            if (this.DesignMode)
                return;
            if (!this._ZoomFlag)
            {
                this.TabPageReset(tp);
                return;
            }
            float inerwith = this.InnerSize.Width;
            float inerhigh = this.InnerSize.Height;
            float scale = (((float)tp.Width) / inerwith > ((float)tp.Height) / inerhigh) ? ((float)tp.Height) / inerhigh : ((float)tp.Width) / inerwith;
            int x = (int)(((float)tp.Width - scale * inerwith) / 2f);
            int y = (int)(((float)tp.Height - scale * inerhigh) / 2f);
            Point p = new Point(x, y);

            if (!(scale > 0))
                return;
            //重置控件原点，并按照比例放大控件
            foreach (Control con in tp.Controls)
            {
                if (con.Dock == DockStyle.None)
                {
                    if (con is IBHXJZOOM)
                    {
                        IBHXJZOOM zoom = con as IBHXJZOOM;
                        zoom.OriginPoint = p;
                        if (this._ZoomLocation)
                            zoom.BHXJLocationZoom(scale);
                        else
                            zoom.BHXJLocationZoom(1);
                        if (this._ZoomSize)
                            zoom.BHXJSizeZoom(scale);
                        else
                            zoom.BHXJSizeZoom(1);
                        if (this._ZoomFont)
                            zoom.BHXJFontZoom(scale);
                        else
                            zoom.BHXJFontZoom(1);
                    }

                    BHXJDOTNET.NEW.BHXJComPass cp = con as BHXJDOTNET.NEW.BHXJComPass;
                    if (cp != null)
                    {
                        int a = 0;
                    }

                }
            }
        }


        /// <summary>
        /// 子控件（Control）的标题文字变动事件触发的函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Control_TextChanged(object sender, EventArgs e)
        {
            SetItemSize(null);
        }
        /// <summary>
        /// 字体变化事件触发的函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BHXJ_FontChanged(object sender, EventArgs e)
        {
            SetItemSize(null);
        }
        
        /// <summary>
        /// 添加子控件触发的函数
        /// </summary>
        /// <param name="e"></param>
        protected override void OnControlAdded(ControlEventArgs e)
        {
            base.OnControlAdded(e);
            if (e.Control is TabPage)
            {
                e.Control.TextChanged += new EventHandler(Control_TextChanged);
                e.Control.SizeChanged += new EventHandler(TabPage_SizeChanged);
                if (this.DesignMode)
                {
                    //e.Control.BackColor = this._BackgroundColor;
                    //e.Control.BackColor = Color.Transparent;
                    //e.Control.BackColor = this._SelectedTabColor;
                }

                SetItemSize(null);
            }
        }
        /// <summary>
        /// 移除子控件触发的函数
        /// </summary>
        /// <param name="e"></param>
        protected override void OnControlRemoved(ControlEventArgs e)
        {
            base.OnControlRemoved(e);
            if (e.Control is TabPage)
            {
                e.Control.TextChanged -= new EventHandler(Control_TextChanged);
                e.Control.SizeChanged -= new EventHandler(TabPage_SizeChanged);
                SetItemSize((TabPage)e.Control);
            }
        }
        /// <summary>
        /// 绘制函数
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Rectangle TabControlArea = this.ClientRectangle;
            Rectangle TabArea = this.DisplayRectangle;
            Graphics gs = e.Graphics;
            #region 背景
            
            Brush SelectItemBrush = new SolidBrush(_SelectedTabColor);
            //gs.FillRectangle(BackgroundBrush, 0, 0, this.Width, this.Height);
            gs.FillRectangle(SelectItemBrush, 0, 0, this.Width, this.Height);
           
            #endregion

            #region 边框分割线
            //边框
            if (this._HasFrame)
            {
                Pen edge = new Pen(this._FrameColor);
                gs.DrawLine(edge, 0, 0, this.Width - 1, 0);
                gs.DrawLine(edge, 0, 0, 0, this.Height - 1);
                gs.DrawLine(edge, 0, this.Height - 1, this.Width - 1, this.Height - 1);
                gs.DrawLine(edge, this.Width - 1, 0, this.Width - 1, this.Height - 1);
                edge.Dispose();
            }
            //分割线
            {
                Brush BackgroundBrush = new SolidBrush(_BackgroundColor);

                Pen edge = new Pen(this._SelectedTabColor,1);
                int WholeItemHH = this.ItemSize.Height * this.RowCount;
                //switch (this.Alignment)
                //{
                //    case TabAlignment.Top:
                //        gs.DrawLine(edge, 1, WholeItemHH + 3, this.Width - 2, WholeItemHH + 3);
                //        break;
                //    case TabAlignment.Bottom:
                //        gs.DrawLine(edge, 1, this.Height - WholeItemHH - 4, this.Width - 2, this.Height - WholeItemHH - 4);
                //        break;
                //    case TabAlignment.Left:
                //        gs.DrawLine(edge, WholeItemHH + 3, 1, WholeItemHH + 3, this.Height - 1);
                //        break;
                //    case TabAlignment.Right:
                //        gs.DrawLine(edge, this.Width - WholeItemHH - 4, 1, this.Width - WholeItemHH - 4, this.Height - 1);
                //        break;
                //}
                switch (this.Alignment)
                {
                    case TabAlignment.Top:
                        //gs.DrawLine(edge, 1, WholeItemHH + 1, this.Width - 2, WholeItemHH + 1);
                        gs.FillRectangle(BackgroundBrush, 1, 1, this.Width - 2, WholeItemHH);
                        break;
                    case TabAlignment.Bottom:
                        //gs.DrawLine(edge, 1, this.Height - WholeItemHH - 2, this.Width - 2, this.Height - WholeItemHH - 2);
                        gs.FillRectangle(BackgroundBrush, 1, this.Height - WholeItemHH - 1, this.Width - 2, WholeItemHH);
                        break;
                    case TabAlignment.Left:
                        //gs.DrawLine(edge, WholeItemHH + 1, 1, WholeItemHH + 1, this.Height - 2);
                        gs.FillRectangle(BackgroundBrush, 1, 1, WholeItemHH, this.Height - 2);
                        break;
                    case TabAlignment.Right:
                        //gs.DrawLine(edge, this.Width - WholeItemHH - 2, 1, this.Width - WholeItemHH - 2, this.Height - 2);
                        gs.FillRectangle(BackgroundBrush, this.Width - WholeItemHH - 1, 1, WholeItemHH, this.Height - 2);
                        break;
                }

                BackgroundBrush.Dispose();
            }

            #endregion
            //TextureBrush tTabBg = new TextureBrush(RES.Skin.TabTitle);
            //gs.FillRectangle(tTabBg, 0, 0, this.Width, RES.Skin.TabTitle.Height);
            //tTabBg.Dispose();
            //王


            for (int i = 0; i < this.TabPages.Count; i++)
            {
                bool bSelected = (this.SelectedIndex == i);

                SolidBrush Selectbrush = new SolidBrush(_SelectedTabColor);

                SolidBrush Normalbrush = new SolidBrush(_NormalTabColor);

                Brush SelectFontBrush = new SolidBrush(_SelectedFontColor);

                Brush NormalFontBrush = new SolidBrush(_NormalFontColor);

                switch (_TabShape)
                {
                    case ItemShape.Rectangle:
                        {
                            if (bSelected) PaintRectangleItem(gs, i, Selectbrush, SelectFontBrush);
                            else
                                PaintRectangleItem(gs, i, Normalbrush, NormalFontBrush);
                        }
                        break;
                    case ItemShape.Circular:
                        {
                            if (bSelected) PaintRoundRectangleItem(gs, i, _TabRadius, Selectbrush, SelectFontBrush);
                            else PaintRoundRectangleItem(gs, i, _TabRadius, Normalbrush, NormalFontBrush);
                        }
                        break;
                }

                Selectbrush.Dispose();
                Normalbrush.Dispose();
                SelectFontBrush.Dispose();
                NormalFontBrush.Dispose();
            }
        }
        /// <summary>
        /// 设置标签尺寸
        /// </summary>
        /// <param name="removePage">不以该tabpage的text属性作为设置依据</param>
        private void SetItemSize(TabPage removePage)
        {
            Graphics gs = this.CreateGraphics();
            //高（按照字体计算）
            float h = this.FontHeight * 1.618f * this._ItemHighScale;
            //宽（按照最长文本长度计算）
            float w = 0;
            for (int i = 0; i < this.TabPages.Count; ++i)
            {
                if (!this.TabPages[i].Equals(removePage))
                {
                    SizeF textarea = gs.MeasureString(this.TabPages[i].Text, this.Font);
                    w = (int)((w > textarea.Width) ? w : textarea.Width);
                }
            }
            w = w * 1.0618f * this._ItemWidthScale;

            //this._TabRadius = (int)(this.FontHeight * 1.618 / 6);
            switch (this.Alignment)
            {
                case TabAlignment.Left:
                case TabAlignment.Right:
                    this.ItemSize = new Size((int)(h), (int)(w));
                    break;
                case TabAlignment.Bottom:
                case TabAlignment.Top:
                    this.ItemSize = new Size((int)(w), (int)(h));
                    break;
            }
        }
        /// <summary>
        /// 绘制Circular标签
        /// </summary>
        /// <param name="gs"></param>
        /// <param name="index">要绘制的标签的序号</param>
        /// <param name="radius">TabRadius</param>
        /// <param name="brush">标签背景画刷</param>
        /// <param name="FontBrush">标签文字画刷</param>
        private void PaintRoundRectangleItem(Graphics gs, int index, int radius, SolidBrush brush, Brush FontBrush)
        {
            Rectangle recBounds = this.GetTabRect(index);
            RectangleF tabTextArea = recBounds;
            StringFormat stringFormat = new StringFormat();
            stringFormat.Alignment = StringAlignment.Center;
            stringFormat.LineAlignment = StringAlignment.Center;

            bool bSelected = (this.SelectedIndex == index);
            if (!bSelected)
            {
                //if (brush.Color != this._BackgroundColor)
                if (index == this.mousemoveindex)
                    this.DrawRoundRectangle(gs, brush, recBounds.Location.X, recBounds.Location.Y, recBounds.Width, recBounds.Height,radius);
            }
            else
            {
                switch (this.Alignment)
                {
                    case TabAlignment.Top:
                        {
                            this.DrawRoundRectangleTop(gs, brush, recBounds.Location.X, recBounds.Location.Y, recBounds.Width, recBounds.Height, radius);
                        }
                        break;
                    case TabAlignment.Bottom:
                        {
                            this.DrawRoundRectangleBottom(gs, brush, recBounds.Location.X, recBounds.Location.Y, recBounds.Width, recBounds.Height, radius);
                        }
                        break;
                    case TabAlignment.Left:
                        {
                            this.DrawRoundRectangleLeft(gs, brush, recBounds.Location.X, recBounds.Location.Y, recBounds.Width, recBounds.Height, radius);
                        }
                        break;
                    case TabAlignment.Right:
                        {
                            this.DrawRoundRectangleRight(gs, brush, recBounds.Location.X, recBounds.Location.Y, recBounds.Width, recBounds.Height, radius);
                        }
                        break;
                }
            }
            gs.DrawString(this.TabPages[index].Text, this.Font, FontBrush, tabTextArea, stringFormat);
        }


        private void DrawRoundRectangle(Graphics g, Brush b, float x, float y, float width, float height, float radius)
        {
            width=width-1;
            height=height-1;
            if (width < 1 || height < 1)
                return;
            using (GraphicsPath gp = new GraphicsPath())
            {
                gp.AddLine(x + radius, y, x + width - radius, y);
                gp.AddArc(x + width - (radius * 2), y, radius * 2, radius * 2, 270, 90);
                gp.AddLine(x + width, y + radius, x + width, y + height - radius);
                gp.AddArc(x + width - (radius * 2), y + height - (radius * 2), radius * 2, radius * 2, 0, 90); // Corner
                gp.AddLine(x + width - radius, y + height, x + radius, y + height);
                gp.AddArc(x, y + height - (radius * 2), radius * 2, radius * 2, 90, 90);
                gp.AddLine(x, y + height - radius, x, y + radius);
                gp.AddArc(x, y, radius * 2, radius * 2, 180, 90);

                gp.CloseFigure();
                SmoothingMode ss = g.SmoothingMode;
                g.SmoothingMode = SmoothingMode.AntiAlias;


                g.FillPath(b, gp);

                g.SmoothingMode = ss;
            }
        }

        private void DrawRoundRectangleTop(Graphics g, Brush b, float x, float y, float width, float height, float radius)
        {
            width = width - 1;
            height = height - 1;
            if (width < 1 || height < 1)
                return;
            using (GraphicsPath gp = new GraphicsPath())
            {
                gp.AddLine(x + radius, y, x + width - radius, y);
                gp.AddArc(x + width - (radius * 2), y, radius * 2, radius * 2, 270, 90);

                gp.AddLine(x + width, y + radius, x + width, y + height);

                gp.AddLine(x, y + height, x, y + radius);
                gp.AddArc(x, y, radius * 2, radius * 2, 180, 90);

                gp.CloseFigure();
                SmoothingMode ss = g.SmoothingMode;
                g.SmoothingMode = SmoothingMode.AntiAlias;


                g.FillPath(b, gp);

                g.SmoothingMode = ss;
            }
        }

        private void DrawRoundRectangleBottom(Graphics g, Brush b, float x, float y, float width, float height, float radius)
        {
            width = width - 1;
            height = height - 1;
            if (width < 1 || height < 1)
                return;
            using (GraphicsPath gp = new GraphicsPath())
            {
                gp.AddLine(x + width, y, x + width, y + height - radius);
                gp.AddArc(x + width - (radius * 2), y + height - (radius * 2), radius * 2, radius * 2, 0, 90); // Corner
                gp.AddLine(x + width - radius, y + height, x + radius, y + height);
                gp.AddArc(x, y + height - (radius * 2), radius * 2, radius * 2, 90, 90);
                gp.AddLine(x, y + height - radius, x, y);

                gp.CloseFigure();
                SmoothingMode ss = g.SmoothingMode;
                g.SmoothingMode = SmoothingMode.AntiAlias;


                g.FillPath(b, gp);

                g.SmoothingMode = ss;
            }
        }

        private void DrawRoundRectangleLeft(Graphics g, Brush b, float x, float y, float width, float height, float radius)
        {
            width = width - 1;
            height = height - 1;
            if (width < 1 || height < 1)
                return;
            using (GraphicsPath gp = new GraphicsPath())
            {
                gp.AddLine(x + radius, y, x + width, y);

                //gp.AddLine(x + width, y, x + width, y + height);

                gp.AddLine(x + width, y + height, x + radius, y + height);
                gp.AddArc(x, y + height - (radius * 2), radius * 2, radius * 2, 90, 90);
                gp.AddLine(x, y + height - radius, x, y + radius);
                gp.AddArc(x, y, radius * 2, radius * 2, 180, 90);

                gp.CloseFigure();
                SmoothingMode ss = g.SmoothingMode;
                g.SmoothingMode = SmoothingMode.AntiAlias;


                g.FillPath(b, gp);

                g.SmoothingMode = ss;
            }
        }

        private void DrawRoundRectangleRight(Graphics g, Brush b, float x, float y, float width, float height, float radius)
        {
            width = width - 1;
            height = height - 1;
            if (width < 1 || height < 1)
                return;
            using (GraphicsPath gp = new GraphicsPath())
            {
                gp.AddLine(x, y, x + width - radius, y);
                gp.AddArc(x + width - (radius * 2), y, radius * 2, radius * 2, 270, 90);
                gp.AddLine(x + width, y + radius, x + width, y + height - radius);
                gp.AddArc(x + width - (radius * 2), y + height - (radius * 2), radius * 2, radius * 2, 0, 90); // Corner
                gp.AddLine(x + width - radius, y + height, x, y + height);
                //gp.AddLine(x, y + height, x, y);

                gp.CloseFigure();
                SmoothingMode ss = g.SmoothingMode;
                g.SmoothingMode = SmoothingMode.AntiAlias;


                g.FillPath(b, gp);

                g.SmoothingMode = ss;
            }
        }
        /// <summary>
        /// 绘制Rectangle标签
        /// </summary>
        /// <param name="gs"></param>
        /// <param name="index">要绘制的标签的序号</param>
        /// <param name="brush">标签背景画刷</param>
        /// <param name="FontBrush">标签文字画刷</param>
        private void PaintRectangleItem(Graphics gs, int index, SolidBrush brush, Brush FontBrush)
        {
            Rectangle recBounds = this.GetTabRect(index);
            RectangleF tabTextArea = (RectangleF)recBounds;
            StringFormat stringFormat = new StringFormat();
            stringFormat.Alignment = StringAlignment.Center;
            stringFormat.LineAlignment = StringAlignment.Center;

            //if (brush.Color != this._BackgroundColor)
            if (index == this.SelectedIndex||index==this.mousemoveindex)
                gs.FillRectangle(brush, recBounds.Location.X + 1, recBounds.Location.Y + 1, recBounds.Width - 2, recBounds.Height - 2);
            
            gs.DrawString(this.TabPages[index].Text, this.Font, FontBrush, tabTextArea, stringFormat);

            //gs.DrawRectangle(new Pen(Color.Red), recBounds.Location.X + 2, recBounds.Location.Y + 2, recBounds.Width - 4, recBounds.Height - 4);
        }
        /// <summary>
        /// TabShape枚举
        /// </summary>
        public enum ItemShape
        {
            Rectangle, Circular
        }
    }

}
